﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using UFSSWebApp.UFSSservicereference;

namespace UFSSWebApp
{
    public partial class userpage : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private ArrayList sublist = new ArrayList();
        private UFSSservicereference.UFSSSubscription[] subs;
        private UFSSservicereference.UFSSFile[] filearray;
        private ArrayList files = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["id"] != null)
                try
                {
                    Session.Remove("user");
                    Session.Add("user", Request.QueryString["id"]);
                }
                catch
                {
                    Response.Redirect("Error.aspx");
                }

            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            subs = ussc.getUserSubs(Session["user"].ToString());
            filearray = ussc.getUserFiles(Session["user"].ToString());

            if (ussc.isFriend(uc, Session["user"].ToString()))
                btnRequest.Text = "Remove Friend";
            else
                btnRequest.Text = "Request Friend";

            if (!IsPostBack)
                refresh();
        }

        private void refresh()
        {
            try
            {
                lstFiles.Enabled = true;
                lstFriends.Enabled = true;
                lstSubs.Enabled = true;

                files.Clear();
                sublist.Clear();

                lstFiles.Items.Clear();
                lstFriends.Items.Clear();
                lstSubs.Items.Clear();

                lblError.Text = "";

                ArrayOfString info = new ArrayOfString();
                info = ussc.getOtherUserInfo(Session["user"].ToString());
                //info
                lblFName.Text = info[1].ToString();
                //lblLName.Text = info[2].ToString();
                lblSubbedTo.Text = info[5].ToString();
                lblNumFiles.Text = ussc.getOtherUserStat(Session["user"].ToString(), "FileCount").ToString();
                lblNumSubs.Text = ussc.getOtherUserStat(Session["user"].ToString(), "SubCount").ToString();
                lblNumFriends.Text = ussc.getOtherUserStat(Session["user"].ToString(), "FriendCount").ToString();

                //files
                try
                {
                    foreach (UFSSservicereference.UFSSFile file in filearray)
                    {
                        files.Add(file);
                        lstFiles.Items.Add(file.filename.ToString());
                    }

                    if (lstFiles.Items.Count == 0)
                    {
                        lstFiles.Items.Add("No Files");
                        lstFiles.Enabled = false;
                    }
                }
                catch
                {
                    lstFiles.Items.Add("No Files");
                    lstFiles.Enabled = false;
                }

                //friends
                try
                {
                    string[] friends = ussc.getUserFriends(Session["user"].ToString()).ToArray<string>();
                    int FriendCount = friends.Length;

                    for (int i = 0; i < FriendCount; i++)
                        lstFriends.Items.Add(friends[i]);

                    if (lstFriends.Items.Count == 0)
                    {
                        lstFriends.Items.Add("No Friends");
                        lstFriends.Enabled = false;
                    }
                }
                catch
                {
                    lstFriends.Items.Add("No Friends");
                    lstFriends.Enabled = false;
                }

                //subs
                try
                {
                    for (int i = 0; i < subs.Length; i++)
                        sublist.Add(subs[i]);

                    foreach (UFSSservicereference.UFSSSubscription sub in sublist)
                        lstSubs.Items.Add(sub.subName);

                    if (lstSubs.Items.Count == 0)
                    {
                        lstSubs.Items.Add("No Subscriptions");
                        lstSubs.Enabled = false;
                    }
                }
                catch
                {
                    lstSubs.Items.Add("No Subscriptions");
                    lstSubs.Enabled = false;
                }
            }
            catch
            {
                Response.Redirect("Error.aspx");
            }
        }

        protected void btnViewFriend_Click(object sender, EventArgs e)
        {
            if (lstFriends.SelectedIndex != -1)
            {
                Session.Remove("user");
                Session.Add("user", lstFriends.SelectedItem.ToString());
                Response.Redirect("~/userpage.aspx");
            }
            else
                lblError.Text = "Please select a friend to view.";
        }

        protected void btnViewSub_Click(object sender, EventArgs e)
        {
            if (lstSubs.SelectedIndex != -1)
            {
                Session.Remove("sub");
                Session.Add("sub", lstSubs.SelectedItem.ToString());
                Response.Redirect("~/usersubs.aspx");
            }
            else
                lblError.Text = "Please select a subscription to view.";
        }

        protected void btnViewFile_Click(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
            {
                Session.Remove("fileinfo");
                Session.Add("fileinfo", filearray[lstFiles.SelectedIndex]);
                Response.Redirect("~/userfile.aspx");
            }
            else
                lblError.Text = "Please select a file to view.";
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            Session.Remove("reciever");
            Session.Add("reciever", lstFriends.SelectedItem.ToString());
            Response.Redirect("~/message.aspx");
        }

        protected void btnRequest_Click(object sender, EventArgs e)
        {
            if (btnRequest.Text.Equals("Request Friend"))
            {
                try
                {
                    string friendname;
                    friendname = Session["user"].ToString();
                    if (ussc.addFriend(uc, friendname))
                        lblError.Text = "Friend request sent!";
                    else
                        lblError.Text = "Error requesting friend";

                    refresh();
                }
                catch
                {
                    lblError.Text = "Error requesting friend";
                }
            }
            else
            {
                try
                {
                    ussc.delFriend(uc, Session["user"].ToString());
                    refresh();
                }
                catch
                {
                }
            }
        }
    }
}